/*=+--+=#=+--     Unmanned Aerial System Management Software      --+=#=+--+=#*\
|          Copyright (C) 2011 Regents of the University of Colorado.           |
|                             All Rights Reserved.                             |

     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License version 2 as
     published by the Free Software Foundation.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

            Jack Elston                       Cory Dixon                        
|           elstonj@colorado.edu              dixonc@colorado.edu              |
|                                                                              |
\*=+--+=#=+--                 --+=#=+--+=#=+--                    --+=#=+--+=#*/

/***********************************************************************
 *			
 * FILENAME:
 * commandStruct.h
 *
 * PURPOSE:
 * Command structures for communication between base station and 
 * Naiad network.
 * 
 * LAST MODIFIED:
 * $Date: 2005/09/06 20:53:06 $
 * $Revision: 1.6 $
 * $Author: elstonj $
 ***********************************************************************/ 

#ifndef _COMMANDSTRUCT_NAIAD_H
#define _COMMANDSTRUCT_NAIAD_H

#include <inttypes.h>
#include "sensorStruct.h"

//////////////////////////////////////////////////////////////////////
// Status Bits
//////////////////////////////////////////////////////////////////////
// bit 0 - Off/onn
// bit 1 - Disconnected/Connected
// bit 2 - working/error
// bit 3 - polled/streamed
//
// size of (char *)this is same as an macine int 
// which is 32 bits
//////////////////////////////////////////////////////////////////////
struct Status {
	unsigned on        : 1;
	unsigned connected : 1;
	unsigned error     : 1;
	unsigned mode      : 1;
	unsigned unused    : 4;
} __attribute__ ((packed));

enum StatusMode { STATUS_POLLED=0, STATUS_STREAM };

//-------------------------------------------------------------------- 
// helper functions
//-------------------------------------------------------------------- 
#define CopyBufToStruct(buf, me) memcpy((char *)&me, buf, sizeof(me) )
#define CopyStructToBuf(me, buf)   memcpy(buf, (char *)&me, sizeof(me) )

//////////////////////////////////////////////////////////////////////
// Naiad definitions
// !!!! This means it is referenced to comm naiad, not soekris
//////////////////////////////////////////////////////////////////////
typedef struct
{
	struct Status	status; 	// status of task

	// stats
	uint32_t 	numBytesTx; // number of bytes sent by comm
	uint32_t 	numBytesRx; // number of bytes recieved by comm
	uint16_t 	numDrop;    // number of bytes dropped by comm
	uint32_t 	numCmdRx;   // number of commands recieved by comm
	uint32_t 	numCmdTx;   // number of commands tramsmitted by comm

	// Rates
	float	rateTx; // transmitted
	float	rateRx; // received

	// signal
	uint8_t	signal; // signal strength

} __attribute__ ((packed)) CommStruct;

typedef struct
{
	float lat;        // deg
	float lon;        // deg
	float alt;        // m
	float speed;      // knots
	float track;      // ground heading deg from North
} __attribute__ ((packed)) MNRStruct;

//////////////////////////////////////////////////////////////////////
// Soekris definitions
//////////////////////////////////////////////////////////////////////
typedef struct 
{
	// the communication status
	// of the GCS
	uint32_t  numBytesTx;    // number of bytes sent by comm
	uint32_t  numBytesRx;    // number of bytes recieved by comm
	uint32_t  numCmdTx; // number of transmitted commands
	uint32_t  numCmdRx; // number of received commands
	uint16_t  numDrop;  // number of received commands
	float     rateTx;        // transmitted rate
	float     rateRx;        // received rate

	float   ping;
	float   minPing;
	float   avgPing;
	float   maxPing;

	// status - connected, on, mode
	struct Status status;
} __attribute__ ((packed)) IFStruct;

typedef struct
{
	// communication interfaces
	IFStruct sock;
	IFStruct serial;

	float   cpuUsage;		// percentage of CPU used
	float   memUsage;		// percentage of mem used
	float   diskFree;		// number of free bytes 

} __attribute__ ((packed)) SoekrisStruct;

typedef struct
{
	float    linkTime;
	float    deltaT;
	int      rssi;
	int      noise;
	uint32_t numPackets;
	char     mac[13];
} __attribute__ ((packed)) SoekrisLinkStat;

typedef struct {
	struct Status  status;  // status of task

	float lat;        // deg
	float lon;        // deg
	float alt;        // m
	float speed;      // knots
	float track;      // ground heading deg from North
	float pdop;
	float hdop;
	float vdop;
	char  fix;

	float bearing;    // deg from north 
	float distance;   // m to waypoint   
	float deltaAlt;   // m to waypoint   
	uint8_t waypointNum; // current waypoint

	unsigned long epochSec; // number of seconds since 0:0:0 UTC for today
	char utc[10];    // hhmmss.ss
	char date[7];      // ddmmyy
	int8_t quality;      // 0->invalid  1-> fix 2->DGPS
	int8_t sv;         // satellites in view
} __attribute__ ((packed)) GPSStruct;

//////////////////////////////////////////////////////////////////////
// Raytheon definitions
//////////////////////////////////////////////////////////////////////

typedef struct {
	float lat;
	float lon;
	float heading;
	int16_t trueAirSpeed;
	int32_t barometricAlt;
	int8_t availableForCommand;
	int16_t twelveV;
	int16_t roll;
	int16_t pitch;
	int16_t yaw;
	uint8_t waypointValue;
} __attribute__ ((packed))TelemetryPacket_t;

typedef struct {
	PiccoloWaypt0 part0;
	PiccoloWaypt1 part1;
} __attribute__ ((packed))WaypointPacket_t;

typedef struct {
	char id [32];
	union{
		uint32_t data;
		struct {
			unsigned flight_computer   : 1;
			unsigned autopilot         : 1;
			unsigned gps               : 1;
			unsigned imu               : 1;
			unsigned air_data          : 1;
			unsigned comms             : 1;
			unsigned health_and_status : 1;
			unsigned video             : 1;
		};
	};
} __attribute__ ((packed))Capabilities_t;


/*typedef struct {
  WaypointPacket_t waypoint_list [4];
  } __attribute__ ((packed))FlightPlanPacket_t;*/

typedef struct {
	float phase_angle;
} __attribute__ ((packed))Coordinate_t;
#endif // _COMMANDSTRUCT_H
